# Maintainer: Andreas 'Segaja' Schleifer <archlinux at segaja dot de>

_gemname='puma'
pkgname="ruby-$_gemname"
pkgver=5.6.5
pkgrel=2
pkgdesc='A Ruby/Rack web server built for concurrency'
arch=('loong64' 'x86_64')
url='https://puma.io/'
license=('BSD-3-CLAUSE')
options=(!emptydirs)
depends=('ruby' 'ruby-nio4r' 'ruby-sd_notify')
makedepends=('ruby-bundler' 'ruby-minitest' 'ruby-minitest-proveit' 'ruby-minitest-retry' 'ruby-minitest-stub-const' 'ruby-rack' 'ruby-rake' 'ruby-rake-compiler' 'ruby-rdoc')
source=("https://github.com/puma/puma/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz"
        "${pkgname}_fix_tests.patch")
sha512sums=('0ad41f3372732bdea6594a20b116ec4fc03a8a1d2ff807b3c315e7769aef4e5361ff775975575125002fbfc83315e6906fa8691ce666161bd8beee8c469289d8'
            'da06b0dafb1ff45d79b3dff05c53334ba1ef2f4f8f8edd904d6418961da51e78f1dff5e1aee19154699651a8ea5c1241b8018eab3f1769fb163a3609b1ac5df0')

prepare() {
  cd "${_gemname}-${pkgver}"

  # remove some tests and fix others
  patch --verbose --strip=1 --input="../${pkgname}_fix_tests.patch"

  # we built based on a tar archive, not a git repo
  sed --in-place 's/git ls-files/find/' "${_gemname}.gemspec"

  # update gemspec/Gemfile to allow newer version of the deps
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec" Gemfile

  # disable rubocop, we don't have that in our repos and it doesn't provide any benefits during building
  sed --in-place '/rubocop/Id' Rakefile Gemfile

  # disable m. Only required for debugging during development
  sed --in-place "/'m'/d" Gemfile

  # disable localhost. Only required for self-signed certificates during development
  sed --in-place "/localhost/d" Gemfile
}

build() {
  cd "${_gemname}-${pkgver}"

  rake compile
  rake build
}

check() {
  cd "${_gemname}-${pkgver}"

  # https://github.com/puma/puma/blob/master/CONTRIBUTING.md#file-limits
  ulimit -Sn 5000

  # some seeds cause random errors ( https://github.com/puma/puma/issues/2825 ) so we use a fixed seed
  export TESTOPTS="--seed=21082"

  rake test:all
}

package() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "${pkgdir}/${_gemdir}" \
    --bindir "${pkgdir}/usr/bin" \
    "pkg/${_gemname}-${pkgver}.gem"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "${pkgdir}/${_gemdir}/cache/" \
    "${pkgdir}/${_gemdir}/gems/${_gemname}-${pkgver}/vendor/" \
    "${pkgdir}/${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

  find "${pkgdir}/${_gemdir}/gems/" \
    -type f \
    \( \
      -iname "*.o" -o \
      -iname "*.c" -o \
      -iname "*.so" -o \
      -iname "*.time" -o \
      -iname "gem.build_complete" -o \
      -iname "Makefile" \
    \) \
    -delete

  find "${pkgdir}/${_gemdir}/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete

  install -D --mode=644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install -D --mode=644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
